from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.generics import ListAPIView
from . import serializers
from rest_framework.views import APIView
from . import models
# Create your views here.
class Register(APIView):
    def post(self,request,*args,**kwargs):
        serializer = serializers.RegisterSer(data=request.data)
        serializer.is_valid(raise_exception=True)
        print(serializer.attrs)
        models.User.objects.create_user(**serializer.attrs)
        return Response(
            data={
                'status': 200,
                'msg': '注册成功',
            }
        )


class Login(APIView):
    def post(self,request,*args,**kwargs):
        serializer = serializers.LoginSer(data=request.data)
        serializer.is_valid(raise_exception=True)
        return Response(data={
            'username': serializer.username,
            'status': 200
        },status=200
        )

class Add_order(APIView):
    def post(self,request,*args,**kwargs):
        serializer = serializers.OrderSer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(data={
            'msg': '下单成功',
            'status': 200,
        })

class Order_list(ListAPIView):
    queryset = models.Order.objects.filter().all()
    serializer_class = serializers.OrderListSer

class Modify_order(APIView):
    def post(self,request,*args,**kwargs):
        pk = request.data.get('pk')
        models.Order.objects.filter(pk=pk).update(order_status=2)
        return Response(data={
            'msg': '接单成功',
            'status': 200,
        })

